package com.example.zhu95.mytimber.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.adapters.ItemVpopupAdapter;
import com.example.zhu95.mytimber.entity.AlbumBean;
import com.example.zhu95.mytimber.entity.SongBean;
import com.example.zhu95.mytimber.entity.SongData;
import com.example.zhu95.mytimber.utils.MyComparator;
import com.example.zhu95.mytimber.utils.Util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.example.zhu95.mytimber.ConstantValue.ALBUM_ID;

/**
 * @author zhu95
 * @date 2018/3/4
 */
public class AlbumDetailFragment extends BaseFragment {
    View view;
    Toolbar toolbar;
    ImageView imgAlbumHeader;
    RecyclerView recyclerViewAlbumDetail;
    LinearLayoutManager layoutManager;
    List<SongBean> songs = new ArrayList<>();
    ItemVpopupAdapter adapter;
    int albumid;

    public List<SongBean> getSongs() {
        return songs;
    }

    public void setSongs(List<SongBean> songs) {
        this.songs = songs;
        adapter.setSongs(songs);
        adapter.notifyDataSetChanged();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.fragment_album_detail, container, false);
        toolbar = view.findViewById(R.id.toolbar);
        imgAlbumHeader = view.findViewById(R.id.img_album_header);
        recyclerViewAlbumDetail = view.findViewById(R.id.recycler_view_album_detail);
        layoutManager = new LinearLayoutManager(rootActivity);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerViewAlbumDetail.setLayoutManager(layoutManager);

        rootActivity.setSupportActionBar(toolbar);
        ActionBar actionBar = rootActivity.getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        MenuItem goArtistItem = menu.add(R.string.go_artist);
        goArtistItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                rootActivity.getArtistClickListener().clickArtist(songs.get(0).getArtistId());
                return false;
            }
        });
        menu.findItem(R.id.action_viewas).setVisible(false);
        menu.findItem(R.id.action_order_by).setVisible(true);
        menu.findItem(R.id.sort_songnum).setVisible(false);
        menu.findItem(R.id.sort_album).setVisible(false);
        menu.findItem(R.id.sort_artist).setVisible(true);
        menu.findItem(R.id.view_default).setVisible(true);
        menu.findItem(R.id.action_search).setIcon(R.drawable.ic_search_white_24dp);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            rootActivity.onBackPressed();
            return true;
        }
        try {
            switch (item.getItemId()) {
                case R.id.sort_az:
                    Collections.sort(songs, new MyComparator(SongBean.class.getDeclaredField("title")));
                    break;
                case R.id.sort_za:
                    Collections.sort(songs, new MyComparator(SongBean.class.getDeclaredField("title"), true));
                    break;
                case R.id.sort_year:
                    Collections.sort(songs, new MyComparator(SongBean.class.getDeclaredField("year")));
                    break;
                case R.id.sort_artist:
                    Collections.sort(songs, new MyComparator(SongBean.class.getMethod("getArtist")));
                    break;
                case R.id.sort_duration:
                    Collections.sort(songs, new MyComparator(SongBean.class.getDeclaredField("duration")));
                    break;
                default:
            }
            notifyDataSetChanged();
        } catch (NoSuchFieldException | NoSuchMethodException e) {
            e.printStackTrace();
        }

        return super.onOptionsItemSelected(item);
    }

    public void notifyDataSetChanged() {
        adapter.setSongs(songs);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        Bundle bundle = getArguments();
        albumid = bundle.getInt(ALBUM_ID);
        AlbumBean album = SongData.findAlbumByID(albumid);
        toolbar.setTitle(album.getAlbum());
        String albumArt = album.getAlbumArt();
        // if (!TextUtils.isEmpty(albumArt)) {
        Glide.with(rootActivity)
                .load(albumArt)
                .error(R.drawable.ic_empty_music2)
                .centerCrop()
                .into(imgAlbumHeader);
        /*} else {
            Glide.with(rootActivity).load(R.drawable.ic_empty_music2).centerCrop().into(imgAlbumHeader);
        }*/
        imgAlbumHeader.setColorFilter(getResources().getColor(Util.INSTANCE.getRandomForeColor()));

        songs.clear();
        for (SongBean song : SongData.songs) {
            if (song.getAlbumId() == albumid) {
                songs.add(song);
            }
        }
        if (adapter == null) {
            adapter = new ItemVpopupAdapter(songs, ItemVpopupAdapter.ItemType.ALBUM, rootActivity);
            recyclerViewAlbumDetail.setAdapter(adapter);
        } else {
            recyclerViewAlbumDetail.setAdapter(adapter);
            notifyDataSetChanged();
        }
    }
}
